﻿//=============================================================
// Copyright (C) 2011 深圳市中旭企业管理股份有限公司
// 作 者：江怀玉
// 日 期：2011/11/17
// 描 述: 通用的查询数据功能[YUI]
// URL Parameter：
//        entity:对应的实体
//        condition:过滤数据条件
//        order:排序字段
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2011/11/17   江怀玉           
//=============================================================
using System;
using System.Linq;
using System.Text;
using ZX.Web.UI.Controls;
using ZX.DBModule;

using ZX.BLL.Extend;
using System.Collections.Generic;
using System.Collections;
using ZX.Common.Extend;


namespace ZX.WebApp.Extend
{
    public partial class QueryForm : BasePage
     {
        protected void Page_Load(object sender, EventArgs e)
        {
            setSolution();
        }
        
        public string FieldInfo
        {
            get
            {
                return setFieldInfo();
            }
        }

        private string setFieldInfo()
        {
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            BaseEntity instance = ZX.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Extend.BaseBussnies(instance);
            bInstance.Order = "iOrder";
            IList<DataListTemplate> list = bInstance.GetEntitys<DataListTemplate>("EntityName='" + strEntity + "'");
            if (list == null)
            {
                return "[]";
            }
            return ZX.Common.Globals.ObjectToJSON(list);
        }

        /// <summary>
        /// 
        /// </summary>
        public string strJson
        {
            get
            {
                //if (IndexOf("QueryForm") > -1)
                {
                    ZX.DBModule.QuerySolution entity = new QuerySolution();
                    return ZX.Common.Globals.ObjectToJSON(entity);
                }

            }
        }


        /// <summary>
        /// 设置默认查询
        /// </summary>
        public string DefineCondition
        {
            get;
            set;
        }

        /// <summary>
        /// 解决方案
        /// </summary>
        public string Solution
        {
            get;
            set;
        }


        public string SolutionList
        {
            get;
            set;
        }
        private void setSolution()
        {
            ZX.BLL.Extend.BaseBussnies bll = new BLL.Extend.BaseBussnies();
            IList<QuerySolution> list = bll.GetEntitys<QuerySolution>("AddUser='"+this.UserId+"' and FunctionID='" + this.FunctionID + "'");
            if (list != null)
            {
                
                QuerySolution entity = list.Where(p => p.IsDefault == "Y").FirstOrDefault();
                DefineCondition = entity == null ? "" : entity.Condition;
                StringBuilder select = new StringBuilder("<select id='HistorySolution' onchange='ChangeSolution(this);'>");
                 select.Append("<option value=\""+(new Guid())+"\"></option>");
                foreach (var o in list)
                {
                    select.Append(string.Format("<option value=\"{0}\">{1}</option>", o.QueryId, o.SolutionName));
                }
                select.Append("</select>");
                Solution = select.ToString();
                SolutionList = ZX.Common.Globals.ObjectToJSON(list);
            }
            else
            {
                StringBuilder select = new StringBuilder("<select id='HistorySolution'>");
                select.Append("<option value=\"" + (new Guid()) + "\"></option>");
                select.Append("</select>");
                Solution = select.ToString();
                SolutionList = "[]";
            }
        }
    }
}